﻿using System;
using System.Configuration;
using System.Globalization;
using System.Text;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Minification.YuiCompressor.Config
{
  internal interface IJavaScriptOptions
  {
    Boolean VerboseLogging { get; }
    Boolean ObfuscateJavascript { get; }
    Boolean PreserveAllSemicolons { get; }
    Boolean DisableOptimizations { get; }
    Int32 LineBreakPosition { get; }
    Encoding Encoding { get; }
    CultureInfo ThreadCulture { get; }
    Boolean IgnoreEval { get; }
  }

  internal class JavaScriptConfigElement : ConfigurationElement, IJavaScriptOptions
  {
    private Encoding _encoding;
    private CultureInfo _threadCulture;

    [ConfigurationProperty("isVerboseLogging", IsRequired = false, DefaultValue = true)]
    public Boolean VerboseLogging { get { return (Boolean)base["isVerboseLogging"]; } }

    [ConfigurationProperty("isObfuscateJavascript", IsRequired = false, DefaultValue = true)]
    public Boolean ObfuscateJavascript { get { return (Boolean)base["isObfuscateJavascript"]; } }

    [ConfigurationProperty("preserveAllSemicolons", IsRequired = false, DefaultValue = false)]
    public Boolean PreserveAllSemicolons { get { return (Boolean)base["preserveAllSemicolons"]; } }

    [ConfigurationProperty("disableOptimizations", IsRequired = false, DefaultValue = false)]
    public Boolean DisableOptimizations { get { return (Boolean)base["disableOptimizations"]; } }

    [ConfigurationProperty("lineBreakPosition", IsRequired = false, DefaultValue = -1)]
    public Int32 LineBreakPosition { get { return (Int32)base["lineBreakPosition"]; } }

    [ConfigurationProperty("encoding", IsRequired = false, DefaultValue = null)]
    public String ContentEncoding { get { return (String)base["encoding"]; } }

    [ConfigurationProperty("threadCulture", IsRequired = false, DefaultValue = "en-GB")]
    public String ThreadCulture { get { return (String)base["threadCulture"]; } }

    [ConfigurationProperty("isEvalIgnored", IsRequired = false, DefaultValue = false)]
    public Boolean IgnoreEval { get { return (Boolean)base["isEvalIgnored"]; } }

    Encoding IJavaScriptOptions.Encoding { get { return _encoding ?? (_encoding = String.IsNullOrEmpty(ContentEncoding) ? Encoding.Default : Encoding.GetEncoding(ContentEncoding)); } }
    CultureInfo IJavaScriptOptions.ThreadCulture { get { return _threadCulture ?? (_threadCulture = CultureInfo.GetCultureInfo(ThreadCulture)); } }
  }
}
